home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-17 | 4.1 KB | 211 lines | [TEXT/CWIE] |
- #include "BoyerMoore.h"
- #include <string.h>
- #include <ctype.h>
-
- BoyerMoore::BoyerMoore(void)
- {
- fStringLen = 0;
- fSearchDataLen = 0;
- fStartingOffset = 0;
- fSearchDataVoid = nil;
- fSearchData = nil;
- }
-
-
- BoyerMoore::BoyerMoore(char* searchStr, short searchLen, void* searchData, long searchDataLen)
- {
- SetSearchString(searchStr, searchLen);
-
- fStartingOffset = 0;
- fSearchData = nil;
-
- if (searchData == nil)
- {
- fSearchDataVoid = nil;
- fSearchDataLen = 0;
- }
- else {
- fSearchDataVoid = searchData;
- fSearchDataLen = searchDataLen;
- }
-
- BuildSkipTable();
- }
-
-
- BoyerMoore::~BoyerMoore(void)
- {
-
- }
-
-
- void BoyerMoore::SetSearchString(char *searchStr, short searchLen)
- {
- memmove(fSearchString, searchStr, searchLen);
- fStringLen = searchLen;
-
- BuildSkipTable();
- }
-
-
- void BoyerMoore::SetSearchString(Str255 searchStr)
- {
- memmove(fSearchString, &searchStr[1], searchStr[0]);
- fStringLen = searchStr[0];
-
- BuildSkipTable();
- }
-
-
- void BoyerMoore::SetSearchData(void *searchData, long dataLen)
- {
- fSearchDataVoid = searchData;
- fSearchDataLen = dataLen;
-
- fStartingOffset = 0;
- }
-
-
- #ifdef NEVER
- long BoyerMoore::Search(void) // Assumes all parameters have been setup
- {
- long dataIndex;
- long dataIndexLimit;
- short searchIndex;
- long result = -1;
-
-
- PrepareSearchData();
-
- dataIndexLimit = fSearchDataLen - fStartingOffset;
- for (dataIndex = 0; dataIndex < dataIndexLimit; dataIndex++)
- {
- if (dataIndex < fStringLen) // We could still match
- {
- for (searchIndex = fStringLen - 1; searchIndex <= 0; searchIndex--)
- {
- if (fSearchData[dataIndex] != fSeachString[searchIndex]) // Jump ahead
- {
- dataIndex += fSkipTable[fSearchString[searchIndex]];
- }
- else // Characters matched, let us try the next character
-
- }
- }
- else // Not enough data to make a full match
- break; // Leave the dataIndex loop with result == -1
-
- }
- }
- #endif
-
-
- long BoyerMoore::Search(void) // Assumes all parameters have been setup
- {
- long dataIndex;
- long dataIndexLimit;
- short searchIndex;
- long result = -1;
- Boolean matchFail;
-
- PrepareSearchData();
-
- int i, j, t, M, N;
-
- M = fStringLen;
- N = fSearchDataLen - fStartingOffset;
-
- for (i = M-1, j = M-1; j> 0; i--, j--)
- {
- do
- {
- if (fCaseInsensitive)
- matchFail = tolower(fSearchString[j]) != tolower(fSearchData[i]);
- else
- matchFail = fSearchString[j] != fSearchData[i];
- // fSearchString[j] != fSearchData[i])
- if (matchFail) // If they didn't match
- {
- t = fSkipTable[fSearchData[i]]; // Skip some characters
- i += (M-j > t) ? M-j : t;
- if (i >= N) // If we can't match in remaining length
- return (result); // return the failure value
- j = M-1;
- }
- }
- while (matchFail);
- }
- result = i;
-
- CleanupSearchData();
- return (result);
- }
-
-
- long BoyerMoore::Search(long startingOffset, char *searchStr, short searchLen) // Where we want to start searching
- {
- long result;
-
- if (searchStr != nil)
- SetSearchString(searchStr, searchLen);
-
- fStartingOffset = startingOffset;
-
- result = Search();
- return (result);
- }
-
-
- long BoyerMoore::Search(long startingOffset, Str255 searchStr)
- {
- long result;
-
- fStartingOffset = startingOffset;
- if (searchStr != nil)
- SetSearchString(searchStr);
-
- result = Search();
- return (result);
- }
-
-
- // If you passed in a Handle for fSearchDataVoid, then you could override this
- // method and lock the handle before starting the search (although this doesn't
- // move memory and you shouldn't need to.
- void BoyerMoore::PrepareSearchData(void) // Setup the interall data for searching
- {
- fSearchData = ((unsigned char*)fSearchDataVoid) + fStartingOffset;
- }
-
-
- void BoyerMoore::CleanupSearchData(void) // Let it go if it was locked or marked
- {
- // You could unlock the handle here if you wanted
- }
-
- void BoyerMoore::BuildSkipTable(void)
- {
- short i;
- short skipLen;
- short index;
-
-
- for (i = 0; i < 255; i++) // Initialize the table with basic jump values
- {
- fSkipTable[i] = fStringLen;
- }
-
- // now walk through the search string to
- // figure out how far we can slide the string if we failed
- // to match the last character
- for (i = 0; i < fStringLen; i++)
- {
- skipLen = fStringLen - i - 1;
- index = fSearchString[i];
- fSkipTable[index] = skipLen;
- }
-
- }
-
-
-